package com.eclipse.training.extensions;

import java.net.URL;
import java.util.ArrayList;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.eclipse.training.extensions"; //$NON-NLS-1$

	public static final String IMAGE_SORT = "image.action.sort"; //$NON-NLS-1$
	public static final String IMAGE_XML = "image.xml"; //$NON-NLS-1$
	public static final String IMAGE_ADD = "image.action.add"; //$NON-NLS-1$
	public static final String IMAGE_REM = "image.action.rem"; //$NON-NLS-1$
	public static final String IMAGE_TRAINING = "image.training"; //$NON-NLS-1$

	private ArrayList<IExtension> extensionsRegistry = new ArrayList<IExtension>();

	// The shared instance
	private static Activator plugin;

	/**
	 * The constructor
	 */
	public Activator() {		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		
		initializeExtensionsRegistry();
		
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	@Override
	protected void initializeImageRegistry(ImageRegistry registry) {
		registerImage(registry, IMAGE_SORT, "sort.gif"); //$NON-NLS-1$
		registerImage(registry, IMAGE_XML, "filenav_nav.gif"); //$NON-NLS-1$
		registerImage(registry, IMAGE_ADD, "add.gif"); //$NON-NLS-1$
		registerImage(registry, IMAGE_REM, "delete.gif"); //$NON-NLS-1$
		registerImage(registry, IMAGE_TRAINING, "training.png"); //$NON-NLS-1$
	}

	public ArrayList<IExtension> getExtensionsRegistry() {
		return extensionsRegistry;
	}

	private void initializeExtensionsRegistry() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint point = registry
				.getExtensionPoint(ExtensionParser.EXTENSION_ID);

		if (point != null) {
			IExtension[] extensions = point.getExtensions();
			for (IExtension extension : extensions) {
				if (extension.isValid())
					extensionsRegistry.add(extension);
			}
		}
	}

	private void registerImage(ImageRegistry registry, String key,
			String fileName) {
		try {
			IPath path = new Path("icons/" + fileName); //$NON-NLS-1$
			URL url = FileLocator.find(getBundle(), path, null);
			if (url != null) {
				ImageDescriptor desc = ImageDescriptor.createFromURL(url);
				registry.put(key, desc);
			}
		} catch (Exception e) {
			LogManager.getLogger().logError(e);
		}
	}

	public Image getImage(String key) {
		return getImageRegistry().get(key);
	}

	public ImageDescriptor getImageDescriptor(String key) {
		return getImageRegistry().getDescriptor(key);
	}
}
